package com.gtpickupapp;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Text;

import android.app.Activity;
import android.content.Intent;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ProfilePage extends Activity {

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {    
		MenuInflater inflater = getMenuInflater();    
		inflater.inflate(R.menu.mainmenu, menu);    
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {    
		// Handle item selection    
		switch (item.getItemId()) {    
		case R.id.profile:        
			profile();
			return true;    
		case R.id.logout:        
			logout();
			return true;
		case R.id.home:
			home();
			return true;
		default:        
			return super.onOptionsItemSelected(item);    
		
		}
	}
	
	public void logout(){
		//TODO will probably need to close DB connection and remove users info on logout
		Intent myintent = new Intent(ProfilePage.this,gtpickupapp.class);
		ProfilePage.this.startActivity(myintent);
		finish();
	}
	
	public void profile(){
		Bundle b = getIntent().getExtras();
		Bundle bundle = new Bundle();
		bundle.putString("uName", b.getString("uName"));
		Intent myintent = new Intent(ProfilePage.this,RealProfilePage.class);
		myintent.putExtras(bundle);
		ProfilePage.this.startActivity(myintent);
		finish();
	}
	
	public void home(){
		//TODO make this return the user to the "home" page with all of the information reset in each tab
		Bundle b = getIntent().getExtras();
		Bundle bundle = new Bundle();
		bundle.putString("uName", b.getString("uName"));
		Intent myintent = new Intent(ProfilePage.this,MainPage.class);
		myintent.putExtras(bundle);
		ProfilePage.this.startActivity(myintent);
		finish();
	}
	
	@Override  
	 public void onBackPressed() {  
		 //SearchEventsGroup.group.back();
		 profile();
	     return;  
	 }
	
	
	
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profilepage);
		
		ImageView image = (ImageView) findViewById(R.id.profile_image);
		
		Bundle bundle = getIntent().getExtras();
		TextView username = (TextView) findViewById(R.id.profile_username_txt);
		TextView name = (TextView) findViewById(R.id.profile_name_txt);
		EditText sex = (EditText) findViewById(R.id.profile_sex_txt);
		EditText age = (EditText) findViewById(R.id.profile_age_txt);
		EditText weight = (EditText) findViewById(R.id.profile_weight_txt);
		EditText bench = (EditText) findViewById(R.id.profile_bench_txt);
		EditText squat = (EditText) findViewById(R.id.profile_squat_txt);
		EditText pi = (EditText) findViewById(R.id.profile_pi_txt);
		
        username.setText(bundle.getString("uName"));
        name.setText(bundle.getString("name"));
        sex.setText(bundle.getString("sex"));
        age.setText(bundle.getString("age"));
        weight.setText(bundle.getString("weight"));
        bench.setText(bundle.getString("bench"));
        squat.setText(bundle.getString("squat"));
        pi.setText(bundle.getString("pi"));
        
        
        final Button cancel = (Button) findViewById(R.id.profile_cancel);
        cancel.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));
        cancel.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		//below code is to test using a bundle to pass info from one activity to the next
        		Bundle bundle = getIntent().getExtras();
        		Bundle b = new Bundle();
        		b.putString("uName", bundle.getString("uName"));
        		
        		Intent myintent = new Intent(ProfilePage.this,RealProfilePage.class);
        		myintent.putExtras(b);
        		ProfilePage.this.startActivity(myintent);
        		
        		finish();
        	}         
        });
        
        
        final Button save = (Button) findViewById(R.id.profile_save);
        save.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));
        save.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		String result = "";
        		InputStream is = null;
        		Bundle bundle = getIntent().getExtras();
        		Bundle b = new Bundle();
        		b.putString("uName", bundle.getString("uName"));
        		TextView username = (TextView) findViewById(R.id.profile_username_txt);
        		TextView name = (TextView) findViewById(R.id.profile_name_txt);
        		EditText sex = (EditText) findViewById(R.id.profile_sex_txt);
        		EditText age = (EditText) findViewById(R.id.profile_age_txt);
        		EditText weight = (EditText) findViewById(R.id.profile_weight_txt);
        		EditText bench = (EditText) findViewById(R.id.profile_bench_txt);
        		EditText squat = (EditText) findViewById(R.id.profile_squat_txt);
        		EditText pi = (EditText) findViewById(R.id.profile_pi_txt);
        		
        		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        		nameValuePairs.add(new BasicNameValuePair("userName",username.getText().toString()));
        		nameValuePairs.add(new BasicNameValuePair("sex",sex.getText().toString()));
        		nameValuePairs.add(new BasicNameValuePair("age",age.getText().toString()));
        		nameValuePairs.add(new BasicNameValuePair("weight",weight.getText().toString()));
        		nameValuePairs.add(new BasicNameValuePair("piMile",pi.getText().toString()));
        		nameValuePairs.add(new BasicNameValuePair("maxBench",bench.getText().toString()));
        		nameValuePairs.add(new BasicNameValuePair("maxSquat",squat.getText().toString()));
        		
        		try{
        		        HttpClient httpclient = new DefaultHttpClient();
        		        HttpPost httppost = new HttpPost("http://gtplay.webatu.com/editProfile.php");
        		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        		        HttpResponse response = httpclient.execute(httppost);
        		        HttpEntity entity = response.getEntity();
        		        is = entity.getContent();
        		                        		
        		}catch(Exception e){
        		        Log.e("log_tag", "Error in http connection "+e.toString());
        		}
        		
        		
        		Intent myintent = new Intent(ProfilePage.this,RealProfilePage.class);
        		myintent.putExtras(b);
        		ProfilePage.this.startActivity(myintent);
        		
        		finish();
        	}         
        });
        
        
	}
	
	
}
//sex age weight benchmax squatmax pi mile time

//Bundle bundle = getIntent().getExtras();
//String username = bundle.getString("uName");